﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.Results;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.BLL;

namespace ZHDJ.AppInterface.Controllers
{
    public class RelationshipTransferController : ApiController
    {
        #region 提交组织关系转移申请
        [HttpPost]
        public ReturnResult Save(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            PartyRelationshipTransfer model = new PartyRelationshipTransfer();
            string LoginID = Convert.ToString(obj.LoginID);
            model.ID = StringHelper.NewID;
            model.Explain = Convert.ToString(obj.Explain);
            model.Applicant = LoginID;
            model.CreateTime = DateTime.Now;
            model.Status = "审批中";
            model.TageBranchID = Convert.ToString(obj.TageBranchID);
            model.FormerBranchID = Convert.ToString(obj.FormerBranchID);
            model.Age = Convert.ToInt32(obj.Age);
            model.FromBranchAddress = Convert.ToString(obj.FromBranchAddress);
            model.FromBranchFax = Convert.ToString(obj.FromBranchFax);
            model.FromBranchMobileNo = Convert.ToString(obj.FromBranchMobileNo);
            model.MemberCostTime = Convert.ToString(obj.MemberCostTime);
            model.FromBranchZip = Convert.ToString(obj.FromBranchZip);
            model.SerialNumber = Convert.ToString(obj.SerialNumber);
            model.ValidTime = 10;//有效期默认为10天
            model.Type = Convert.ToInt32(obj._Type);

            if (InterfaceHelper.GetApplying(LoginID).Tables[0].Rows.Count > 0)
            {
                result.code = "201";
                result.msg = "你已经申请了一次组织关系转移,不能重复申请";
                return result;
            }

            PartyOrgMember member = InterfaceHelper.GetPartyBuildingUserModel(LoginID);
            if (member.UserBranchId == model.TageBranchID)
            {
                result.code = "201";
                result.msg = "不能在同一组织内进行转移，请悉知";
                return result;
            }

            //区外转区内
            var fromBranchID = "";
            if (model.Type == 2)
                fromBranchID = "D658070EC3BF425BA8417745FDCBFCB0";//山海天ID
            else
                fromBranchID = model.TageBranchID;

            if (InterfaceHelper.SaveRelationshipTransfer(model) == 0)
            {
                result.code = "201";
                result.msg = "提交组织关系申请失败";
                return result;
            }
            if (InterfaceHelper.AddRelationshipBranch(model.ID, Convert.ToInt32(model.Type)) == 0)
            {
                result.code = "201";
                result.msg = "提交组织关系支部失败";
                return result;
            }

            PartyRelationshipProcesses processe = new PartyRelationshipProcesses();
            processe.ID = StringHelper.NewID;
            processe.ApplyID = model.ID;
            processe.Approver = LoginID;
            processe.Explain = model.Explain;
            processe.ApprovalResult = "提交申请";
            processe.CreateTime = DateTime.Now;
            processe.StepInfo = "1";

            if (InterfaceHelper.SavePartyRelationshipProcesses(processe) == 0)
            {
                result.code = "201";
                result.msg = "提交组织关系转移审核步骤详情失败";
                return result;
            }

            string[] approval = new string[] { fromBranchID };
            //组织
            PartyTaskHelper.AddTaskToBranch(model.ID, member.UserName + "组织关系转移申请", SysEnum.TaskCategory.RelationshipTransfer,
                LoginID, approval,
                "../../../../App/PersonalCenter/Module/RelationshipTransfer/RelationshipTransferApproval.aspx?RelationID=" + model.ID);

            //短信提醒
            //申请人
            string msgcontent = string.Format("{0}同志：你的组织关系转移申请已提交，办理状态可登录山海天党建信息平台查看。",member.UserName);
            SMSHelper.CreateSmsSendRecord(member.MobileNo, msgcontent, "组织关系转移申请");
            //代办人短信提醒
            DataTable dtMembers = InterfaceHelper.GetPartyOrgMembersByBranchIDAndIsPartyWorker(fromBranchID, true);
            msgcontent = "{0}同志：您有一个{1}的组织关系转移申请需处理，请您登录山海天党建信息平台处理。";
            foreach (DataRow dr in dtMembers.Rows)
            {
                SMSHelper.CreateSmsSendRecord(dr["MobileNo"].ToString(), string.Format(msgcontent, dr["UserName"].ToString(), member.UserName), "组织关系转移申请处理");
            }

            result.code = "200";
            result.msg = "申请已提交，请耐心等待审核结果";
            return result;
        }
        #endregion

        #region 获取组织关系转接申请
        [HttpPost]
        public ReturnResult GetRelationshipTransfer(dynamic obj)
        {
            DataTable dt = new DataTable();
            IList<PartyRelationshipTransfer> list = new List<PartyRelationshipTransfer>();
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string loginId = Convert.ToString(obj.CreateBy);

            dt = InterfaceHelper.GetGetRelationshipTransfer(pageIndex, pageSize, loginId);
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyRelationshipTransfer>.ConvertToModel(dt);

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = dt.Rows.Count.ToString()
            };
        }
        #endregion
    }
}
